package db.tables;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import com.db.BeanBase;
import com.db.Table;

import db.beans.M_DefinitionBean;

public class M_DefinitionTable extends Table{
	
	public static final String TABLE_NAME = "IF_TABLE";
	public static final String COL_IF_TYPE = "IF_TYPE";
	public static final String COL_CODE = "CODE";
	public static final String COL_NAME = "NAME";
	public static final String COL_TYPE = "TYPE";
	public static final String COL_REQUIRED = "REQUIRED";
	public static final String COL_LENGTH = "LENGTH";
	public static final String COL_TABLE_NAME = "TABLE_NAME";
	public static final String COL_DATA_SOURCE_TYPE = "DATA_SOURCE_TYPE";
	public static final String COL_DATA_SOURCE_DETAIL = "DATA_SOURCE_DETAIL";
	public static final String COL_MATCH_KEY = "MATCH_KEY";
	
	
	public static BeanBase[] selectAll(Connection conn) throws SQLException{
		final String sql = "SELECT " 
			+ COL_IF_TYPE + ","
			+ COL_CODE + ","
			+ COL_NAME + ","
			+ COL_REQUIRED + ","
			+ COL_TYPE + ","
			+ COL_LENGTH + ","
			+ COL_TABLE_NAME +","
			+ COL_DATA_SOURCE_TYPE + ","
			+ COL_DATA_SOURCE_DETAIL + ","
			+ COL_MATCH_KEY 
			+ " FROM " + TABLE_NAME
			;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		Vector<M_DefinitionBean> v = new Vector<M_DefinitionBean>();
        try{
    		stmt = conn.prepareStatement(sql);
    		rs = stmt.executeQuery();
    		int i = 1;
            while( rs.next() ) {
            	i = 1;
            	M_DefinitionBean rec = new M_DefinitionBean();
            	rec.setCode(rs.getInt(++i));
            	rec.setName(rs.getString(++i));
            	rec.setType(rs.getString(++i));
            	v.add(rec);
            }

        } finally {
            if(rs != null){
                rs.close();
            }
            
            if (stmt != null){
            	stmt.close();
            }
        }
        return (M_DefinitionBean[])v.toArray(new M_DefinitionBean[v.size()]);
	}
	
	public static BeanBase[] selectByIFType(Connection conn, String type) throws SQLException{
		final String sql = "SELECT " 
			+ COL_IF_TYPE + ","
			+ COL_CODE + ","
			+ COL_NAME + ","
			+ COL_REQUIRED + ","
			+ COL_TYPE + ","
			+ COL_LENGTH + ","
			+ COL_DATA_SOURCE_TYPE + ","
			+ COL_TABLE_NAME +","
			+ COL_DATA_SOURCE_DETAIL + ","
			+ COL_MATCH_KEY 
			+ " FROM " + TABLE_NAME
			+ " WHERE " + COL_IF_TYPE + "=?"
			;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		Vector<M_DefinitionBean> v = new Vector<M_DefinitionBean>();
        try{
    		stmt = conn.prepareStatement(sql);
    		int i = 0;
    		stmt.setString(++i, type);
    		rs = stmt.executeQuery();
            while( rs.next() ) {
            	i = 0;
            	M_DefinitionBean rec = new M_DefinitionBean();
            	rec.setIfType(rs.getString(++i));
            	rec.setCode(rs.getInt(++i));
            	rec.setName(rs.getString(++i));
            	rec.setRequired(rs.getInt(++i)==1);
            	rec.setType(rs.getString(++i));
            	rec.setLength(rs.getInt(++i));
            	rec.setDataSourceType(rs.getString(++i));
            	rec.setTableName(rs.getString(++i));
            	rec.setDataSourceDetail(rs.getString(++i));
            	rec.setMatchKey(rs.getString(++i));
            	v.add(rec);
            }

        } finally {
            if(rs != null){
                rs.close();
            }
            
            if (stmt != null){
            	stmt.close();
            }
        }
        return (M_DefinitionBean[])v.toArray(new M_DefinitionBean[v.size()]);
	}
}
